package com.example.asus.a222dzd_ordering.fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus.a222dzd_ordering.R;
import com.example.asus.a222dzd_ordering.base.BaseListner;
import com.example.asus.a222dzd_ordering.base.BaseModel;
import com.example.asus.a222dzd_ordering.bean.UserInfoBean;


/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends Fragment{
   TextView tv_name,tv_mobile;
   Button bt_commit;
   SharedPreferences sp;

    public UserFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_user, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tv_name=(TextView)view.findViewById(R.id.userinfo_name);
        tv_mobile=(TextView)view.findViewById(R.id.userinfo_mobile);
        bt_commit=(Button)view.findViewById(R.id.bt_commit);
        initData();
    }

    private void initData() {
        sp=getContext().getSharedPreferences("User", Context.MODE_PRIVATE);
        String userid=sp.getString("userid","000000");

        BaseModel<UserInfoBean> model=new BaseModel<>();
        model.callEnqueue(model.service.getUserInfo(userid), new BaseListner<UserInfoBean>(){
            @Override
            public void onResponse(UserInfoBean userInfoBean) {
                if(userInfoBean.equals("")){
                    Toast.makeText(getContext(), "userfragment获取数据为空", Toast.LENGTH_SHORT).show();
                }else {
                    tv_name.setText(userInfoBean.getUsername());
                    tv_mobile.setText(userInfoBean.getMobilenum());
                }

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
